<?php

class ml_model_dbFans extends Lib_datamodel_db
{
    function __construct() {
        $db_config = ml_factory::load_standard_conf ( 'db' );
        parent::__construct ( 'fans', $db_config ['fans'] );
    }
    
    /**
     * 新增粉丝
     * @param  $uid        被关注人的uid
     * @param  $fans_uid     粉丝的uid         
     */
    function addFans($uid,$fans_uid) {
        if(!$this->init_db($uid))
            return false;
        
        $data = array('uid'=>$uid,'fans_uid'=>$fans_uid,'cip'=>Tool_ip::get_real_ip());
        return $this->insert($data);
        
    }
    
    /**
     * 删除粉丝
     * @param  $uid        被关注人的uid
     * @param  $fans_uid     粉丝的uid
     */
    function delFans($uid,$fans_uid) {
        if(!$this->init_db($uid))
            return false;
    
        $sql = 'DELETE FROM '.$this->table.' WHERE uid = '.$uid.' AND fans_uid = '.$fans_uid;
        return $this->query($sql);    
    }
    
    /**
     * 是否是粉丝
     * @param  $uid        当前登录者uid
     * @param  $follow_uid     粉丝的uid
         
     */
    function isFan($uid,$fan_uid) {
        if(!isset($uid)) {
            return false;
        }
        if(!$this->init_db($uid,self::DB_SLAVE))
            return false;
        
        $where = 'uid = '.$uid.' and fans_uid = '.$fan_uid;                            
        
        return $this->fetch_count($where);
    }
    
    
    
    /**
     * 获取fensi列表
     * @param  $uid        当前登录者uid
     */
    function getList($uid,$begin,$end) {
        if(!$this->init_db($uid,self::DB_SLAVE))
            return false;
        $sql = 'SELECT fans_uid FROM '.$this->table.' where `uid` ='.$uid.' order by ctime desc limit '.$begin.','.$end;
        return $this->query($sql,true);
    }

    
    /**
     * 批量增加粉丝
     * @param  $uid        当前登录者uid
     * @param  $fansUids    粉丝的uid
     */
    /* 
    function addFansBath($uid,$fansUids) {
        if(!$this->init_db($uid))
            return false;
        $ip = Tool_ip::get_real_ip();
        if(is_array($fansUids) && count($fansUids) > 0){
            $value = array();
            foreach($fansUids as $k => $v) {
                $value[] = '('.$uid.', '.$v.', \''.$ip.'\')';
            }
            $val = implode(',', $value);
            $sql = 'INSERT INTO  '.$this->table.' (`uid`,`fans_uid`,`ip`)  VALUES  '.$val ;
        
            return $this->query($sql,true);
        }
    }
     */

    
    public function countFans($uid) {
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $where = " uid= {$uid} ";
        $rs = $this->fetch_count($where);
        if (!$rs) return false;
        return true;
    }

}
